//
//  FirstViewController.m
//  changeViewController
//
//  Created by marcus wang on 12-1-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FirstViewController.h"
#import "Config.h"
#import "Utility.h"
#import "QuartzCore/QuartzCore.h"
#import "FirstViewCell.h"

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.view.backgroundColor=[UIColor clearColor];
        // Custom initialization
    }
    return self;
}



#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    float height=367;
    if (iPhone5) {
        height+=88;
    }
    self.view.frame=CGRectMake(0, 0, self.view.frame.size.width, height);
    self.mytableView=[[UITableView alloc] initWithFrame:self.view.frame];
    self.mytableView.delegate=self;
    self.mytableView.dataSource=self;
    //self.mytableView.showsVerticalScrollIndicator=NO;
    self.mytableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.mytableView];
    self.arrayList=[NSMutableArray array];
    [self.arrayList addObject:@{@"name":@"云吞面",
                  @"content":@"共记录到余震846次",
                 @"list":@[@{@"content": @"从现在到未来的十多年间"}
                ]}];
    [self.arrayList addObject:@{@"name":@"云吞面",
     @"content":@"共记录到余震846次，其中3.0级以上余震57次，包括5.0-5.9级两次。",
     @"list":@[@{@"content": @"从现在到未来的十多年间，西南地区的大型电站水库群，将进入一个密集的建成蓄水时期"},
     @{@"content": @"从现在到未来的十多年间，西南地区的大型电站水库群，将进入一个密集的建成蓄水时期"}
     ]}];
    [self.arrayList addObject:@{@"name":@"云吞面",
     @"content":@"共记录到余震846次，其中3.0级以上余震57次，包括5.0-5.9级两次。",
     @"list":@[@{@"content": @"从现在到未来的十多年间，西南地区的大型电站水库群，将进入一个密集的建成蓄水时期"},
     @{@"content": @"从现在到未来的十多年间，西南地区的大型电站水库群，将进入一个密集的建成蓄水时期"},
     @{@"content": @"从现在到未来的十多年间，西南地区的大型电站水库群，将进入一个密集的建成蓄水时期"}
     ]}];
}

//表视图委托
#pragma mark - Tableview datasource & delegates
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        NSDictionary *dict=[self.arrayList objectAtIndex:section];
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    if (section>0) {
        UILabel * label=[Utility detailTextLabel:CGRectMake(0, 5, 320, 1) andFontSize:1.0];
        label.backgroundColor=[UIColor colorWithRed:235.0/255 green:235.0/255 blue:235.0/255 alpha:1.0];
        [view addSubview:label];
    }
    view.backgroundColor=[UIColor whiteColor];
        //头像
        UIImageView *logo=[[UIImageView alloc] initWithFrame:CGRectMake(6, 12, 40, 40)];
        NSString *logostr=@"";
        if (logostr&&logostr.length>0) {
            logo.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",[Utility getServerUrl],logostr]]]];
        }else{
            logo.image=[Utility imageNamed:@"cg_logo.png"];
        }
        [logo.layer setMasksToBounds:YES];
        [logo.layer setCornerRadius:20.0f];
        [view addSubview:logo];
        //昵称
        UILabel *nickName=[Utility detailTextLabel:CGRectMake(64, 20, 245, 17) andFontSize:12.0f];
        nickName.textColor=UIColorFromRGB(0x000000);
        nickName.numberOfLines=1;
        nickName.text=[dict objectForKey:@"name"];
        [view addSubview:nickName];
        //供稿内容
    NSString *content=[dict objectForKey:@"content"];
    UIFont *font=[UIFont fontWithName:FONT_FAMILY size:12];
    CGSize size=[Utility getStringSize:content font:font withMaxSize:CGSizeMake(256, 100)];
    UILabel *labelContent=[Utility detailTextLabel:CGRectMake(64, 40, 256, size.height) andFontSize:12.0f];
        labelContent.textColor=UIColorFromRGB(0x00000);
    labelContent.text=content;
    labelContent.numberOfLines=(size.height/17);
     [view addSubview:labelContent];
    return view;
 
    
}

//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.arrayList count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  
    NSArray *replyList=[[self.arrayList objectAtIndex:section] objectForKey:@"list"];
    return [replyList count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    UIFont *font=[UIFont fontWithName:FONT_FAMILY size:12.0f];
        NSDictionary *dict=[self.arrayList objectAtIndex:section];
        NSString *pinglunContent=[dict objectForKey:@"content"];
        CGSize size=[Utility getStringSize:pinglunContent font:font withMaxSize:CGSizeMake(250, 100)];
        
        return size.height+43;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   FirstViewCell  *cell = [[FirstViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstViewCell"];
    NSArray *replyList=[[self.arrayList objectAtIndex:indexPath.section] objectForKey:@"list"];
    NSDictionary *dict = [replyList objectAtIndex:indexPath.row];
    UIFont *font=[UIFont fontWithName:FONT_FAMILY size:12.0f];
    CGSize size=[Utility getStringSize:[dict objectForKey:@"content"] font:font withMaxSize:CGSizeMake(230, 100)];
    if (indexPath.row==0) {
        [cell setFrame:CGRectMake(0, 0, 320, size.height+28)];
        UIImageView *sanjiao=[[UIImageView alloc] initWithFrame:CGRectMake(60, 0, 255, 41)];
        sanjiao.image=[Utility imageNamed:@"gonggaocell_bg.png"];
        [cell  insertSubview:sanjiao aboveSubview:cell.backGround];
        cell.backGround.frame=CGRectMake(64, 41, 249, cell
                                         .frame.size.height-41);
        cell.content.frame=CGRectMake(70, 23, 230,  size.height);
        
    }else{
        static NSString *cellIdentity = @"firstViewCell1";
        cell = (FirstViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity];
        if (cell ==  nil) {
            cell = [[FirstViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity];
        }
        [cell setFrame:CGRectMake(0, 0, 320,  size.height+5)];
        cell.backGround.frame=CGRectMake(64, 0, 249, cell
                                         .frame.size.height);
        cell.content.frame=CGRectMake(70, 0, 230, size.height);
    }
    
    cell.content.numberOfLines=size.height/17;
    cell.content.text=[dict objectForKey:@"content"];
    return cell;
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
